package com.example.demo.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class WebCrawlerUtil {
    
    public static void safeCrawlAndPrint(String url) {
        try {
            // 设置超时时间为10秒并忽略内容类型校验
            Document doc = Jsoup.connect(url)
                    .timeout(10000)
                    .ignoreContentType(true)
                    .get();

            // 选择包含文章列表的容器
            Elements articles = doc.select("ul.post-loop.post-loop-default > li.item");

            System.out.println("抓取到 " + articles.size() + " 篇文章：");
            System.out.println("----------------------------------------");

            for (Element article : articles) {
                // 提取标题和链接
                String title = article.selectFirst("h3.item-title a").text(); // 提取标题
                String link = article.selectFirst("h3.item-title a").attr("abs:href"); // 提取完整链接

                // 提取发布时间
                String date = article.selectFirst("div.item-meta span.item-meta-li.date").text();

                // 提取作者信息
                String author = article.selectFirst("div.item-meta div.item-meta-li.author a span").text();

                // 提取阅读数（可选）
                String views = article.selectFirst("div.item-meta div.item-meta-right span.item-meta-li.views").text();

                // 打印结果
                System.out.println("标题：" + title);
                System.out.println("链接：" + link);
                System.out.println("发布时间：" + date);
                System.out.println("作者：" + author);
                System.out.println("阅读数：" + views);
                System.out.println("----------------------------------------");
            }

        } catch (IOException e) {
            System.err.println("抓取失败：" + e.getMessage());
        } catch (Exception e) {
            System.err.println("解析异常：" + e.getMessage());
        }
    }
}
